﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Liquid.Windows.Commands {
	public abstract class CommandBinding : IDisposable {
		public CommandBinding() { }

		public Command Command { get; internal set; }

		protected internal virtual void OnTextChanged(EventArgs e) {
		}
		protected internal virtual void OnImageChanged(EventArgs e) {
		}
		protected internal virtual void OnCheckedStateChanged(EventArgs e) {
		}
		protected internal virtual void OnEnabledChanged(EventArgs e) {
		}
		protected internal virtual void OnToolTipTextChanged(EventArgs e) {
		}

		protected internal virtual void Bind() {
			OnTextChanged(EventArgs.Empty);
			OnImageChanged(EventArgs.Empty);
			OnCheckedStateChanged(EventArgs.Empty);
			OnEnabledChanged(EventArgs.Empty);
			OnToolTipTextChanged(EventArgs.Empty);
		}

		#region IDisposable pattern
		/// <summary>
		/// Dispose of (clean up and deallocate) resources used by this class.
		/// </summary>
		/// <param name="fromUser">
		/// True if called directly or indirectly from user code.
		/// False if called from the finalizer (i.e. from the class' destructor).
		/// </param>
		/// <remarks>
		/// When called from user code, it is safe to clean up both managed and unmanaged objects.
		/// When called from the finalizer, it is only safe to dispose of unmanaged objects.
		/// This method should expect to be called multiple times without causing an exception.
		/// </remarks>
		protected virtual void Dispose(bool fromUser) {
			if (fromUser) {
				GC.SuppressFinalize(this);	// No need for the Finalizer to do all this again.
			}
		}
		/// <summary>
		/// Dispose of all resources (both managed and unmanaged) used by this class.
		/// </summary>
		public void Dispose() {
			// Call our private Dispose method, indicating that the call originated from user code.
			// Diagnostics.TraceInfo("Disposed by user code.");
			this.Dispose(true);
		}
		/// <summary>
		/// Destructor, called by the finalizer during garbage collection.
		/// There is no guarantee that this method will be called. For example, if <see cref="Dispose"/> has already
		/// been called in user code for this object, then finalization may have been suppressed.
		/// </summary>
		~CommandBinding() {
			// Call our private Dispose method, indicating that the call originated from the finalizer.
			// Diagnostics.TraceInfo("Finalizer is disposing CommandBinding instance");
			this.Dispose(false);
		}
		#endregion
	}
}
